package net.chat;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.chat.utils.Encryptor;

/**
 * Class representing a user.
 * @author Prabhat
 * @author Amir added password encryption
 */
public class User {
    // attributes

    private static int lastUid = 0;
    private int uid;
    private String fname;
    private String lname;
    private String pwd;
    private Date lastChat; // time of last message sent

    // full constructor
    public User(String fname, String lname, String pwd) {
        this.uid = lastUid + 1;
        lastUid = this.uid;
        this.fname = fname;
        this.lname = lname;
        try {
            this.pwd = Encryptor.encrypt(pwd);
        } catch (Exception ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // getters
    public int getUid() {
        return uid;
    }

    public String getFname() {
        return fname;
    }

    public String getLname() {
        return lname;
    }

    public String getPwd() {
        return pwd;
    }

    // setters
    public void setUid(int uid) {
        this.uid = uid;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public void setLastChat(Date lastChat) {
        this.lastChat = lastChat;
    }

    public Date getLastChat() {
        return this.lastChat;
    }

    // method to validate user id and password
    public boolean login(int uid, String pwd) {
        return (this.uid == uid && this.pwd.equals(pwd));
    }
}
